<?php

namespace addons\qingdongams\model;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 *发票
 */
class Invoice Extends Model {
    use SoftDelete;
    // 表名,不含前缀
    protected $name = 'qingdongams_invoice';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    public static function getNumber()
    {
        return 'I' . date('ymd') . rand(100, 999);
    }
    public function getImageAttr($value) {
        if($value){
            return cdnurl($value, true);
        }
        return $value;
    }
    //客户
    public function customer() {
        return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name');
    }
    //订单
    public function contract() {
        return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,name,num');
    }
    //负责人
    public function ownerStaff() {
        return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
    }
    public function createStaff() {
        return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
    }
    //添加
    public static function createInvoice($params) {
        foreach ($params as $name => $val) {
            if($params[$name] === ''){
                $params[$name]=NULL;
            }
        }

        $staff                  = Staff::info();
        $staff_id = $staff->id;
        $params['create_staff_id']     = $staff_id;
        $params['owner_staff_id'] = $staff->id;
        $params['check_status'] = 1;
        $flow = Flow::getsteplist(Flow::INVOICE_STATUS);
        $params['flow_id'] = $flow['flow_id'];
        $params['order_id'] = $flow['order_id'];
        if ($flow['status'] == 0) {//发起人自选

            $params['flow_staff_ids'] = trim($params['flow_staff_ids']??'');
        } else {
            $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
        }

        $Model                  = new self;
        $result = $Model->allowField(true)->save($params);
        if (false === $result) {
            // 验证失败 输出错误信息
            throw new Exception($Model->getError());
        }
        $lastId= $Model->getLastInsID();

        $staff_id = explode(',', $params['flow_staff_ids'])[0];

        ExamineRecord::addExaminse(ExamineRecord::INVOICE_TYPE,$lastId, $staff_id);

        return true;
    }


    //编辑
    public static function updateInvoice($params) {
        $params['check_status'] = 1;

        $flow = Flow::getsteplist(Flow::INVOICE_STATUS);
        $params['flow_id'] = $flow['flow_id'];
        $params['order_id'] = $flow['order_id'];

        if ($flow['status'] == 0) {//发起人自选
            if (empty($params['flow_staff_ids'])) {
                throw new Exception('审批人必须选择');
            }
            $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
        } else {
            $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
        }


        $Model                  = new self;
        // 调用当前模型对应的User验证器类进行数据验证
        $result = $Model->save($params,['id'=>$params['id']]);
        if (false === $result) {
            // 验证失败 输出错误信息
            throw new Exception($Model->getError());
        }


        if ($flow['status'] == 1) {//固定审批
            //发送审批通知
            Flow::sendStepRecord($flow,Flow::INVOICE_STATUS, $params['id']);
        } else {//发起人自选 依次审批
            $staff_id   = explode(',', $params['flow_staff_ids'])[0];
            if ($staff_id) {
                ExamineRecord::addExaminse(ExamineRecord::INVOICE_TYPE, $params['id'], $staff_id);
            }
        }

        return true;
    }
    //标记开票
    public static function updateInfo($params){
        $Model                  = new self;
        // 调用当前模型对应的User验证器类进行数据验证
        $result = $Model->save($params,['id'=>$params['id']]);
        if (false === $result) {
            // 验证失败 输出错误信息
            throw new Exception($Model->getError());
        }
        $contractId = $Model->where(['id'=>$params['id']])->value('contract_id');
        if($contractId){
            $file ='';
            if($params['image']){
                $file = File::where(['file_path'=>$params['image']])->value('id');
            }
            $ret = array(
                'invoice_date'=>$params['invoice_time'],
                'invoice_logistics_number'=>$params['logistics'],
                'invoice_file_ids'=>$file,
            );
            Contract::where(['id'=>$contractId])->update($ret);
        }
        return true;
    }
}
